package com.example.mywf.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.mywf.adapter.CityAdapter;
import com.example.mywf.bean.CityBean;
import com.example.mywf.utils.DBUtils;
import com.example.mywf.R;

import java.util.List;

public class CityActivity extends AppCompatActivity {

    //控件
    private ListView city_lv;
    private ImageButton search_btn;
    private TextView location_tv;
    private List<CityBean> cityBeanList;
    private TextView address_tv;
    private Spinner province_sp;
    private Spinner city_sp;
    private Spinner district_sp;
    private ArrayAdapter<String> provinceAdapter;
    private ArrayAdapter<String> cityAdapter;
    private ArrayAdapter<String> districtAdapter;
    //索引
    private int provincePosition = 0;
    private int cityPosition = 0;
    //静态城市信息
    private static final String[] provinceList = new String[]{"北京市", "天津市", "上海市", "重庆市",
            "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省",
            "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "海南省",
            "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "台湾省",
            "内蒙古自治区", "广西壮族自治区", "西藏自治区", "宁夏回族自治区", "新疆维吾尔自治区",
            "香港特别行政区", "澳门特别行政区"};
    private static final String[][] cityList = new String[][]{{"北京市"}, {"天津市"}, {"上海市"}, {"重庆市"},
            /*河北省*/{"石家庄市", "唐山市", "秦皇岛市", "邯郸市", "邢台市", "保定市", "张家口市", "承德市", "沧州市", "廊坊市", "衡水市"},
            /*山西省*/{"太原市", "大同市", "阳泉市", "长治市", "晋城市", "朔州市", "晋中市", "运城市", "忻州市", "临汾市", "吕梁市"},
            /*辽宁省*/{"沈阳市", "大连市", "鞍山市", "抚顺市", "本溪市", "丹东市", "锦州市", "营口市", "阜新市", "辽阳市", "盘锦市", "盘锦市", "朝阳市", "葫芦岛市"},
            /*吉林省*/{"长春市", "吉林市", "四平市", "辽源市", "通化市", "白山市", "松原市", "白城市", "延边"},
            /*黑龙江省*/{"哈尔滨市", "齐齐哈尔市", "鸡西市", "鹤岗市", "双鸭山市", "大庆市", "伊春市", "佳木斯市", "七台河市", "牡丹江市", "黑河市", "绥化市", "大兴安岭地区"},
            /*江苏省*/{"南京市", "无锡市", "徐州市", "常州市", "苏州市", "南通市", "连云港市", "淮安市", "盐城市", "扬州市", "镇江市", "泰州市", "宿迁市"},
            /*浙江省*/{"杭州市", "宁波市", "温州市", "嘉兴市", "湖州市", "绍兴市", "金华市", "衢州市", "舟山市", "台州市", "丽水市"},
            /*安徽省*/{"合肥市", "芜湖市", "蚌埠市", "淮南市", "马鞍山市", "淮北市", "铜陵市", "安庆市", "黄山市", "滁州市", "阜阳市", "宿州市", "六安市", "亳州市", "池州市", "宣城市"},
            /*福建省*/{"福州市", "厦门市", "莆田市", "三明市", "泉州市", "漳州市", "南平市", "龙岩市", "宁德市"},
            /*江西省*/{"南昌市", "景德镇市", "萍乡市", "九江市", "新余市", "鹰潭市", "赣州市", "吉安市", "宜春市", "抚州市", "上饶市"},
            /*山东省*/{"济南市", "青岛市", "淄博市", "枣庄市", "东营市", "烟台市", "潍坊市", "济宁市", "泰安市", "威海市", "日照市", "莱芜市", "临沂市", "德州市", "聊城市", "滨州市", "菏泽市"},
            /*河南省*/{"郑州市", "开封市", "洛阳市", "平顶山市", "安阳市", "鹤壁市", "新乡市", "焦作市", "济源市", "濮阳市", "许昌市", "漯河市", "三门峡市", "南阳市", "商丘市", "信阳市", "周口市", "驻马店市"},
            /*湖北省*/{"武汉市", "黄石市", "十堰市", "宜昌市", "襄阳市", "鄂州市", "荆门市", "孝感市", "荆州市", "黄冈市", "咸宁市", "随州市", "恩施", "仙桃市", "潜江市", "天门市", "神农架林区"},
            /*湖南省*/{"长沙市", "株洲市", "湘潭市", "衡阳市", "邵阳市", "岳阳市", "常德市", "张家界市", "益阳市", "郴州市", "永州市", "怀化市", "娄底市", "湘西"},
            /*广东省*/{"广州市", "韶关市", "深圳市", "珠海市", "汕头市", "佛山市", "江门市", "湛江市", "茂名市", "肇庆市", "惠州市", "梅州市", "汕尾市", "河源市", "阳江市", "清远市", "东莞市", "中山市", "东沙群岛", "潮州市", "揭阳市", "云浮市"},
            /*海南省*/{"海口市", "三亚市", "三沙市", "五指山市", "琼海市", "儋州市", "文昌市", "万宁市", "东方市", "定安县", "屯昌县", "澄迈县", "临高县", "白沙", "昌江", "乐东", "陵水", "保亭", "琼中"},
            /*四川省*/{"成都市", "自贡市", "攀枝花市", "泸州市", "德阳市", "绵阳市", "广元市", "遂宁市", "内江市", "乐山市", "南充市", "眉山市", "宜宾市", "广安市", "达州市", "雅安市", "巴中市", "资阳市", "阿坝", "甘孜", "凉山"},
            /*贵州省*/{"贵阳市", "六盘水市", "遵义市", "安顺市", "铜仁市", "黔西南", "毕节市", "黔东南", "黔南"},
            /*云南省*/{"昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "丽江市", "普洱市", "临沧市", "楚雄", "红河", "文山", "西双版纳", "大理", "德宏", "怒江", "迪庆"},
            /*陕西省*/ {"西安市", "铜川市", "宝鸡市", "咸阳市", "渭南市", "延安市", "汉中市", "榆林市", "安康市", "商洛市"},
            /*甘肃省*/{"兰州市", "嘉峪关市", "金昌市", "白银市", "天水市", "武威市", "张掖市", "平凉市", "酒泉市", "庆阳市", "定西市", "陇南市", "临夏", "甘南"},
            /*青海省*/{"西宁市", "海东市", "海北", "黄南", "海南", "果洛", "玉树", "海西"},
            /*台湾省*/{"台北市", "高雄市", "台南市", "台中市", "金门县", "南投县", "基隆市", "新竹市", "嘉义市", "新北市", "宜兰县", "新竹县", "桃园县", "苗栗县", "彰化县", "嘉义县", "云林县", "屏东县", "台东县", "花莲县", "澎湖县", "连江县"},
            /*内蒙古自治区*/{"呼和浩特市", "包头市", "乌海市", "赤峰市", "通辽市", "鄂尔多斯市", "呼伦贝尔市", "巴彦淖尔市", "乌兰察布市", "兴安盟", "锡林郭勒盟", "阿拉善盟"},
            /*广西壮族自治区*/{"南宁市", "柳州市", "桂林市", "梧州市", "北海市", "防城港市", "钦州市", "贵港市", "玉林市", "百色市", "贺州市", "河池市", "来宾市", "崇左市"},
            /*西藏自治区*/{"拉萨市", "昌都地区", "山南地区", "日喀则地区", "那曲地区", "阿里地区", "林芝地区"},
            /*宁夏回族自治区*/{"银川市", "石嘴山市", "吴忠市", "固原市", "中卫市"},
            /*新疆维吾尔自治区*/{"乌鲁木齐市", "克拉玛依市", "吐鲁番地区", "哈密地区", "昌吉", "博尔塔拉", "巴音郭楞", "阿克苏地区", "克孜勒苏柯尔克孜自治州", "喀什地区", "和田地区", "伊犁", "塔城地区", "阿勒泰地区", "石河子市", "阿拉尔市", "图木舒克市", "五家渠市"},
            /*香港特别行政区*/{"香港岛", "香港岛", "新界"},
            /*澳门特别行政区*/{"澳门", "离岛"}};
    private static final String[][][] districtList = new String[][][]{
            {/*北京市*/{"朝阳区", "海淀区", "通州区", "房山区", "丰台区", "昌平区", "大兴区", "顺义区", "西城区", "延庆县", "石景山区", "宣武区", "怀柔区", "崇文区", "密云县", "东城区", "平谷区", "门头沟区"}},
            {/*天津市*/{"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "东丽区", "西青区", "津南区", "北辰区", "武清县", "宝坻区", "滨海新区", "宁河区", "静海区", "蓟州区"}},
            {/*上海市*/{"黄埔区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "浦东新区", "金山区", "松江区", "青浦区", "奉贤县", "崇明区"}},
            {/*重庆市*/{"万州区", "涪陵区", "渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "綦江区", "大足区", "渝北区", "巴南区", "黔江区", "长寿区", "江津区", "合川区", "永川区", "南川区", "璧山区", "铜梁区", "潼南区", "荣昌区", "开州区", "梁平区", "武隆区", "城口县", "丰都县", "垫江县", "忠县", "云阳县", "奉节县", "巫山县", "巫溪县", "石柱自治县", "秀山自治县", "酉阳自治县", "彭水自治县"}},
            {},//河北省
            {},//山西省
            {},//辽宁省
            {},//吉林省
            {},//黑龙江省
            {},//江苏省
            {},//浙江省
            {},//安徽省
            {},//福建省
            {},//江西省
            {},//山东省
            {},//河南省
            {},//湖北省
            {/*长沙市*/{"芙蓉区", "天心区", "岳麓区", "开福区", "雨花区", "望城区", "浏阳市", "宁乡市", "长沙县"},
                    /*株洲市*/{"天元区", "芦淞区", "荷塘区", "石峰区", "渌口区", "醴陵市", "攸县", "茶陵县", "炎陵县"},
                    /*湘潭市*/{"雨湖区", "岳塘区", "湘乡市", "韶山市", "湘潭县"},
                    /*衡阳市*/{"雁峰区", "石鼓区", "珠晖区", "蒸湘区", "南岳区", "衡阳县", "衡南县", "衡山县", "衡东县", "祁东县", "耒阳市", "常宁市"},
                    /*邵阳市*/{"双清区", "大祥区", "北塔区", "邵东市", "新邵县", "邵阳县", "隆回县", "洞口县", "绥宁县", "新宁县", "武冈市"},
                    /*岳阳市*/{"岳阳楼区", "云溪区", "君山区", "岳阳县", "华容县", "平江县", "湘阴县"},
                    /*常德市*/{"武陵区", "鼎城区", "安乡县", "汉寿县", "桃源县", "临澧县", "石门县", "澧县", "津市市"},
                    /*张家界市*/{"永定区", "武陵源区", "慈利县", "桑植县"},
                    /*益阳市*/{"资阳区", "赫山区", "沅江市", "安化县", "桃江县", "南县"},
                    /*郴州市*/{"北湖区", "苏仙区", "桂阳县", "宜章县", "永兴县", "嘉禾县", "临武县", "汝城县", "桂东县", "安仁县", "资兴市"},
                    /*永州市*/{"零陵区", "冷水滩区", "双牌县", "祁阳县", "东安县", "道县", "宁远县", "新田县", "蓝山县", "江永县"},
                    /*怀化市*/{"鹤城区", "中方县", "沅陵县", "辰溪县", "溆浦县", "会同县", "麻阳苗族自治县", "新晃侗族自治县", "芷江侗族自治县", "靖州苗族侗族自治县", "通道侗族自治县", "洪江市", "怀化市洪江管理区"},
                    /*娄底市*/{"娄星区", "冷水江市", "涟源市", "双峰县", "新化县"},
                    /*湘西*/{"吉首市", "泸溪县", "凤凰县", "花垣县", "保靖县", "古丈县", "永顺县", "龙山县", "湘西经济开发区"}},//湖南省
            {/*广州市*/{"越秀区", "荔湾区", "海珠区", "天河区", "白云区", "黄埔区", "花都区", "番禺区", "南沙区", "从化区", "增城区"},
                    /*韶关市*/{"浈江区", "武江区", "曲江区", "乐昌市", "南雄市", "始兴县", "仁化县", "翁源县", "新丰县", "乳源瑶族自治县"},
                    /*深圳市*/{"福田区", "罗湖区", "南山区", "盐田区", "宝安区", "龙岗区", "坪山区", "龙华区", "光明区", "大鹏新区", "深汕特别合作区"},
                    /*海珠市*/{"香洲区", "金湾区", "斗门区"},
                    /*汕头市*/{"金平区", "龙湖区", "澄海区", "濠江区", "潮阳区", "潮南区", "南澳县"},
                    /*佛山市*/{"禅城区", "南海区", "顺德区", "高明区", "三水区"},
                    /*江门市*/{"蓬江区", "江海区", "新会区", "台山市", "开平市", "鹤山市", "恩平市"},
                    /*湛江市*/{"赤坎区", "霞山区", "麻章区", "坡头区", "雷州市", "廉江市", "吴川市", "徐闻县", "遂溪县"},
                    /*茂名市*/{"茂南区", "电白区", "高州市", "信宜市", "化州市"},
                    /*肇庆市*/{"端州区", "鼎湖区", "高要区", "广宁县", "怀集县", "封开县", "德庆县", "四会市"},
                    /*惠州市*/{"惠城区", "惠阳区", "惠东县", "博罗县", "龙门县"},
                    /*梅州市*/{"梅江区", "梅县区", "平远县", "蕉岭县", "大埔县", "丰顺县", "五华县"},
                    /*汕尾市*/{"汕城区", "海丰县", "陆河县", "陆丰市"},
                    /*河源市*/{"源城区", "东源县", "龙川县", "紫金县", "连平县", "和平县"},
                    /*阳江市*/{"江城区", "阳东区", "阳西县", "阳春市", "海陵区"},
                    /*清远市*/{"清城区", "清新区", "英德市", "连州市", "佛冈县", "阳山县", "连山壮族瑶族自治县", "连南瑶族自治县"},
                    /*东莞市*/{"莞城街道", "南城街道", "东城街道", "万江街道"},
                    /*中山市*/{"石岐街道", "东区街道", "西区街道", "南区街道", "五桂山街道", "中山港街道"},
                    /*东沙群岛*/{"东沙环礁", "南卫滩环礁", "北卫滩环礁"},
                    /*潮州市*/{"湘桥区", "枫溪区", "潮安区", "饶平县"},
                    /*揭阳市*/{"榕城区", "揭东区", "惠来县", "揭西县"},
                    /*云浮市*/{"云城区", "云安区", "新兴县", "郁南县", "罗定市"}},//广东省
            {},//海南省
            {},//四川省
            {},//贵州省
            {},//云南省
            {},//陕西省
            {},//甘肃省
            {},//青海省
            {},//台湾省
            {},//内蒙古自治区
            {},//广西壮族自治区
            {},//西藏自治区
            {},//宁夏回族自治区
            {},//新疆维吾尔自治区
            {},//香港特别行政区
            {}};//澳门特别行政区

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        //初始化控件
        initView();
        //设置下拉菜单
        setSpinner();
        //获取数据
        cityBeanList = new DBUtils(this).selectmycity();
        //设置listView显示数据
        setCity_lv();
        //设置按钮
        setSearch_btn();
        //获取gps定位信息
        getGPS();
    }

    //返回页面处理
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取数据
        cityBeanList = new DBUtils(this).selectmycity();
        //设置listView显示数据
        setCity_lv();
    }

    //初始化控件
    private void initView() {
        city_lv = findViewById(R.id.city_lv);
        search_btn = findViewById(R.id.search_ibtn);
        location_tv = findViewById(R.id.location_tv);
        address_tv = findViewById(R.id.address_tv);
        province_sp = findViewById(R.id.province_sp);
        city_sp = findViewById(R.id.city_sp);
        district_sp = findViewById(R.id.district_sp);
    }

    //设置下拉菜单
    private void setSpinner() {

        //适创建配器
        provinceAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, provinceList);
        //设置适配器
        province_sp.setAdapter(provinceAdapter);
        province_sp.setSelection(0, true);
        //适创建配器
        cityAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, cityList[0]);
        //设置适配器
        city_sp.setAdapter(cityAdapter);
        city_sp.setSelection(0, true);
        //适创建配器
        districtAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, districtList[0][0]);
        //设置适配器
        district_sp.setAdapter(districtAdapter);
        district_sp.setSelection(0, true);
        //设置监听
        province_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                provincePosition = position;
                cityAdapter = new ArrayAdapter<>(CityActivity.this, android.R.layout.simple_spinner_item, cityList[position]);
                city_sp.setAdapter(cityAdapter);
                city_sp.setSelection(0, true);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //设置监听
        city_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                cityPosition = position;
                districtAdapter = new ArrayAdapter<>(CityActivity.this, android.R.layout.simple_spinner_item, districtList[provincePosition][position]);
                district_sp.setAdapter(districtAdapter);
                district_sp.setSelection(0, true);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //设置监听
        district_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                address_tv.setText(provinceList[provincePosition] + cityList[provincePosition][cityPosition] + districtList[provincePosition][cityPosition][position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //设置listView显示数据
    private void setCity_lv() {
        //创建适配器
        CityAdapter adapter = new CityAdapter(this, R.layout.city_item, cityBeanList);
        //设置适配器
        city_lv.setAdapter(adapter);
        //设置点击事件
        adapter.setOnItemClickListener(new CityAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(CityBean cityBean) {
                System.out.println("点击了" + cityBean.getCityName());
                //创建意图
                Intent intent = new Intent();
                //放入点击的城市代码
                intent.putExtra("cityid", cityBean.getCityId());
                //设置结果码
                setResult(1, intent);
                //页面返回
                CityActivity.this.finish();
            }

            @Override
            public void onCancelClick(CityBean cityBean) {
                //使用数据库工具类删除点击的城市
                new DBUtils(CityActivity.this).deletemycity(cityBean.getCityId());
                //重新获取数据
                cityBeanList = new DBUtils(CityActivity.this).selectmycity();
                //设置listView显示数据
                setCity_lv();
            }
        });
    }

    //设置按钮
    private void setSearch_btn() {
        search_btn.setOnClickListener(v -> {
            Intent intent = new Intent(CityActivity.this, SearchActivity.class);
            startActivityForResult(intent, 1);
        });
    }

    //获取gps定位信息
    private void getGPS() {
        //获取位置服务
        final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        //权限检查
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        //设置监听
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(@NonNull String provider) {

            }

            @Override
            public void onProviderDisabled(@NonNull String provider) {

            }
        });
        //获取位置
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //构造字符
        if (location != null) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("您的位置是：\n");
            stringBuilder.append("经度：");
            stringBuilder.append(location.getLongitude());
            stringBuilder.append("\n纬度：");
            stringBuilder.append(location.getLatitude());
            location_tv.setText(stringBuilder);
        } else {
            location_tv.setText("没有获取到位置信息");
        }
    }
}
